import 'package:banyou_app/app/request/http_request.dart';
import 'package:banyou_app/app/routes/app_pages.dart';
import 'package:banyou_app/app/util/common_utils.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

class HomeController extends GetxController {
  //TODO: Implement HomeController

  final index = 0.obs;
  final showAddress = false.obs;
  final dataList = [].obs;
  final dataList1 = [].obs;
  @override
  void onInit() {
    super.onInit();
  }

  clickIndex(i) {
    this.index.value = i;
  }

  loadData() async {
    final result =
        await HttpRequest.get('https://aqcdn.olbibo.com.cn/Home.json');
    if (result.result) {
      List tempList = result.data ?? [];
      if (tempList.isNotEmpty) {
        int unm = (tempList.length ~/ 2).toInt();
        dataList.value = tempList.sublist(0, unm);
        dataList1.value = tempList.sublist(unm, tempList.length);
        index.refresh();
      }
    }
  }

  openSet() async {
    await openAppSettings();
  }

  toInfo(item) {
    String full = box.read("full") ?? "";

    if (full.isNotEmpty) {
      showMang(full);
    } else {
      Get.toNamed(Routes.USERINFO, arguments: item);
    }
  }

  void requestPermission() async {
    String mobile = box.read('mobile') ?? "";
    String oneKey = box.read('oneKey') ?? "";

    if (oneKey != '' || (mobile != '' && mobile != '16524285975')) {
      channel.invokeMethod('startLocation');
    } else {
      showAddress.value = false;
    }
  }

  @override
  void onReady() {
    loadData();
    requestPermission();

    super.onReady();
  }

  @override
  void onClose() {}
}
